package com.gdut.simple.presenter;

import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.FollowContract;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.IFollowModel;
import com.gdut.simple.model.impl.FollowModelImpl;

public class FollowPresenter implements FollowContract.Presenter {

    private FollowContract.View mFollowView;
    private IFollowModel mFollowModel;

    public FollowPresenter(FollowContract.View mFollowView) {
        this.mFollowView = mFollowView;
        mFollowModel = new FollowModelImpl();
        mFollowView.setPresenter(this);
    }

    @Override
    public void getUser() {
        mFollowModel.getUser(mFollowView.getUserId(), new RequestCallback<User>() {
            @Override
            public void onSuccess(User user) {
                mFollowView.setUser(user);
                mFollowView.refresh(user);
            }

            @Override
            public void onFailure(String message) {
                mFollowView.showMessage(message);
            }
        });
    }

    @Override
    public void checkStatus() {
        mFollowView.hideFollowButton();
        mFollowModel.checkStatus(mFollowView.getUser(), mFollowView.getFollow(), new RequestCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                if (result) {
                    mFollowView.hideFollowButton();
                } else {
                    mFollowView.showFollowButton();
                }
            }

            @Override
            public void onFailure(String message) {
                mFollowView.showMessage(message);
            }
        });
    }

    @Override
    public void follow() {
        if (!mFollowView.isLogin()) {
            mFollowView.showLoginBar();
            return;
        }
        mFollowModel.follow(mFollowView.getUser(), mFollowView.getFollow(), new RequestCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                if (result) {
                    mFollowView.hideFollowButton();
                    mFollowView.showMessage("关注成功");
                } else {
                    mFollowView.showMessage("unknown error");
                }
            }

            @Override
            public void onFailure(String message) {
                mFollowView.showMessage(message);
            }
        });
    }
}
